package com.chinaunicom.jenkins.controller;

import com.chinaunicom.jenkins.entity.User;
import com.chinaunicom.jenkins.service.IUserService;
import com.chinaunicom.jenkins.utils.ResponseResult;
import com.chinaunicom.jenkins.vo.UserVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: ruiyanglee
 * @date: 2020-09-09 09:59
 * @description:
 */
@RestController
@RequestMapping("/user")
public class UserController {
    public static final Logger log = LoggerFactory.getLogger(UserController.class);


    @Autowired
    private IUserService userService;

    @GetMapping
    public ResponseResult list() {
        try {
            List<User> users = userService.list();
            return ResponseResult.ok().message("请求成功").data(users);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return ResponseResult.error().message("请求失败").code(404);
    }


    @DeleteMapping("/{id}")
    public ResponseResult deleteById(@PathVariable String id) {
        try {
            return ResponseResult.ok().message("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return ResponseResult.error().message("删除失败，用户不存在").code(404);
    }

    
    @PutMapping("")
    public ResponseResult pageTeacherCondition(@RequestBody UserVO userVO) {
        try {
            return ResponseResult.ok().message("请求成功").data(null);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return ResponseResult.error().message("请求失败").code(404);
    }

    
    
    @PostMapping("/add")
    public ResponseResult saveTeacher(@RequestBody User user) {
        try {
            userService.save(user);
            return ResponseResult.ok().message("更新成功");
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return ResponseResult.error().message("更新失败").code(404);
    }
}
